package com.base.cn.platform.os.common.utils.web;

import com.base.cn.platform.os.common.constants.CommonConstants;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.j2cache.J2CacheRegion;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 登录工具
 *
 * @author s.li
 * @create 2018-03-13 18:50
 */
@Component
public final class LoginUtil {
    private JsonParser jsonParser = new JsonParser();
    @Autowired
    private CustomCacheChannel customCacheChannel;

    public BigDecimal getLoginUserTeacherId(HttpServletRequest request){
        JsonObject userMap = this.getLoginUser(request);
        if(ObjectUtils.isNotEmpty(userMap)){
            BigDecimal teacherId = userMap.get("teacherId").getAsBigDecimal();
            if(ObjectUtils.isNotEmpty(teacherId)){
                return teacherId;
            }
        }
        return BigDecimal.ZERO;
    }

    /**
     * 登录当前登录的用户ID
     * @param request
     * @return
     */
    public BigDecimal getLoginUserId(HttpServletRequest request){
        JsonObject userMap = this.getLoginUser(request);
        if(ObjectUtils.isNotEmpty(userMap)){
            BigDecimal userId = userMap.get("id").getAsBigDecimal();
            if(DataUtil.idIsNotNull(userId)){
                String _id = userId.toString().split("\\.")[0];
                return new BigDecimal(_id);
            }
        }
        return new BigDecimal(0);
    }

    /**
     * 登录当前登录的用户账号
     * @param request
     * @return
     */
    public String getLoginUserName(HttpServletRequest request){
        JsonObject userMap = this.getLoginUser(request);
        if(ObjectUtils.isNotEmpty(userMap)){
            String userName = userMap.get("username").getAsString();
            return userName;
        }
        return null;
    }

    /**
     * 验证是否是登录
     * @param request
     * @return false未登录，true已登录
     */
    public boolean isLogin(HttpServletRequest request){
        if(ObjectUtils.isNotEmpty(this.getLoginUser(request))){
            return true;
        }
        return false;
    }

    /**
     * 登录登录用户信息
     * @param request
     * @return 用户Map<String,Object>
     */
    public JsonObject getLoginUser(HttpServletRequest request){
        String sin = WebUtils.getCookie(request, CommonConstants.ADMIN_LOGIN_COOKIE);
        if(StringUtils.isNotEmpty(sin)){
            String adminUserStr = (String)customCacheChannel.get(J2CacheRegion.ONE_DAY_REGION.getKey(),sin).getValue();
            if(StringUtils.isNotEmpty(adminUserStr)){
                return jsonParser.parse(adminUserStr).getAsJsonObject();
            }
        }
        return null;
    }
}
